package me.gacl.web.controller;

import java.io.File;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.alibaba.dingtalk.openapi.demo.auth.AuthHelper;
import com.alibaba.dingtalk.openapi.demo.media.MediaHelper;
import com.dingtalk.open.client.api.model.corp.UploadResult;
import com.opensymphony.xwork2.ActionSupport;
import model.CreateWord;
import model.SendToGroup;

public class ToWordAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    private SendToGroup sendtoGroup;
	public SendToGroup getSendtoGroup() {
		return sendtoGroup;
	}
	public void setSendtoGroup(SendToGroup sendtoGroup) {
		this.sendtoGroup = sendtoGroup;
	}
	public String toWord(){
	HttpServletRequest req = ServletActionContext.getRequest();
     Map<String, Object> map = new HashMap<String, Object>();  
   	@SuppressWarnings("unchecked")
	Enumeration<String> paramNames = req.getParameterNames(); 
     // 通过循环将表单参数放入键值对映射中  
     while(paramNames.hasMoreElements()) {  
         String key = paramNames.nextElement();  
         String value = req.getParameter(key);  
         map.put(key, value);  
     }  
    File file = null;  
    
     try {  
         // 调用工具类WordGenerator的createDoc方法生成Word文档  
         file = CreateWord.createDoc(map, "resume"); 
         UploadResult uploadResult;
			try {
				uploadResult = MediaHelper.upload(AuthHelper.getAccessToken(), MediaHelper.TYPE_FILE, file);
				System.out.println(uploadResult.getMedia_id());
				//sendtoGroup.send(uploadResult.getMedia_id());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    
         
     } finally {  
       
     }
	return "success";  
 }  

}
